<link href="Style.css" rel="stylesheet" type="text/css" />
<?php
session_start(); 

require("sql_functions.php");

if(isset($_SESSION['user']))
{       
        $user = $_SESSION['user'];
        
        
        // New milestone or saving milestone
        if(isset($_POST['projectID']))
        {
          
                $pid = $_POST['projectID'];
                $isNew = 0;

                //form info means editting
                if(isset($_POST['formName']) && isset($_POST['formDesc']) && isset($_POST['formDueDate']))
                {
                
                    $mName = $_POST['formName'];
                    $mDesc = $_POST['formDesc'];
                    $mDueDate = makeSQLDate($_POST['formDueDate']);
                    $isComplete = $_POST['formIsComplete'];

                    $mDueDate = makeSQLDate($mDueDate);

                    if(isset($_POST['isNew']) && $_POST['isNew'] == 1)
                    {
                      $ms = sql_createMilestone($pid, $mName, $mDesc, $mDueDate, $isComplete);
                    }
                    else
                    {
                      $oldName = $_POST['oldName'];
                      echo "$oldName";
                      $ms = sql_editMilestone($pid, $oldName, $mName, $mDesc, $mDueDate, $isComplete);
                    }
      
                    $pid = $ms['ProjectID'];
                    $mName = $ms['Name'];

                        header("Location: milestone_info.php?projectID=$pid&MilestoneName=$mName");
                }
                
                //no form means new one
                else
                {       
                    $isNew = 1;
                    $mName = "";
                    $mDesc = "";
                    $mDueDate = makePHPDate("now");
                    $mIsComplete = 0;
                }
        }
        
        
        else if( isset($_GET['projectID']) && isset($_GET['MilestoneName']) )
        {       
            $isNew = 0;
            $pid = $_GET['projectID'];
            $mName = $_GET['MilestoneName'];        
            $ms = sql_getMilestone($pid, $mName);
    
            $mDesc = $ms['Description'];
            $mDueDate = makePHPDate($ms['DueDate']);
            $mIsComplete = $ms['IsComplete'];
    
            $oldName = $mName;
        }
                
        else
        {
            echo "Invalid Milestone";
        }
        
        $project = sql_getProject($pid);
        $pTitle = $project['Title'];       
        
        echo "<html>"; 
        echo "<head>"; 
        echo "</head>"; 
        echo "<body>";
        echo "<p>Milestone Info"; 
        echo "<p>Project: $pTitle ($pid)";
        echo "<form action=\"\" method=\"POST\">"; 
        echo "<p>Name:";
        echo "<input name=\"formName\" value=\"$mName\" />"; 
        echo "</p>"; 
        echo "<p>Description:";  
        echo "<input name=\"formDesc\" value=\"$mDesc\" />"; 
        echo "</p>";
        echo "<p>Due Date:";  
        echo "<input name=\"formDueDate\" value=\"$mDueDate\" />"; 
        echo "</p>"; 
        echo "<p>Complete?:";
        echo "<input type=\"checkbox\" name=\"formIsComplete\" value=\"1\" ";
        
        if($mIsComplete)
        {
          echo "checked=\"yes\" ";
        }
        echo " />";
  
        echo "<input type=\"hidden\" name=\"oldName\" value=\"$oldName\">";
        echo "<input type=\"hidden\" name=\"projectID\" value=\"$pid\">";
        echo "<input type=\"hidden\" name=\"isNew\" value=\"$isNew\">";
  
        echo "<p><input type=\"submit\" name=\"save\" value=\"Save\" />"; 
        echo "</form>";   

        $mTasks = sql_getMilestoneTasks($pid, $mName);

        echo "<p><p>Tasks";                     
        echo "<table border='1'>";      
        echo "<tr><td>Status</td><td>Name</td><td>Assigned To</td>";
        while($row = mysql_fetch_array($mTasks))
        {               
                $tIsClosed = $row['IsClosed'];
                if(!$tIsClosed)
                {
                  $tName = $row['TaskName'];
                  $tDueDate = makePHPDate($row['DueDate']);
                  $tAssignedTo = $row['EmployeeEmail'];     
                  $assignee = sql_getEmployee($tAssignedTo);
                  $firstName = $assignee['FirstName'];
                  $lastName = $assignee['LastName'];

			            $pid = $row['ProjectID'];
			            $project = sql_getProject($pid);
			            $pName = $project['Title'];

                  echo "<tr><td>";
                  echo "Due $tDueDate";
                  echo "</td><td>";     
                  echo "<a href=\"task_info.php?projectID=$pid&MilestoneName=$mName&taskName=$tName\">$tName</a>";
                  echo "</td><td>";
                  echo "$firstName $lastName";
                  echo "</td></tr>";
                }
        }
        echo "</table>";
  

  
  if($isNew)
  {
    echo "Save to create tasks for this milestone";
  }
  else
  {
    echo "<form action=\"task_info.php\" method=\"POST\">";
    echo "<input type=\"hidden\" name=\"projectID\" value=\"$pid\">";
          echo "<input type=\"hidden\" name=\"MilestoneName\" value=\"$mName\">";
    echo "<p><input type=\"submit\" name=\"newTask\" value=\"New Task\" />";
    echo "</form>";
    
  }
  

    echo "<p><a href=\"project_milestones.php?projectID=$pid\">Project Milestones</a>"; 
  
        echo "</body>"; 
        echo "</html>"; 
        
}
else
{
        echo "Must be logged in to view this page.";
}

?>